package it.allerj.gui.action;

import it.allerj.common.persistence.clientSide.*;
import it.allerj.gui.action.GuiActionInterface;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class ActionInvocationHandler implements InvocationHandler{

	GuiActionInterface action;
	boolean closeConnection = true;
	
	public ActionInvocationHandler(GuiActionInterface action)
	{
		this.action = action;
	}
	public ActionInvocationHandler(GuiActionInterface action, boolean closeConnection)
	{
            this.action = action;
            this.closeConnection = closeConnection;
	}
	
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		
		
			//open the hibernate session and store it to a threadlocal variable
			PersistenceClientHome.doInit();
			try {
				return method.invoke(action,args);
			} catch (Exception ex) {
				//ex.printStackTrace();
				throw ex.getCause();
			}
			finally
			{
//			Close the seesio and clear the threadloacla variable
				if(closeConnection)
					PersistenceClientHome.closeAll();
				
				


			}

	}

}
